// pages/groupBuyingList/groupBuyingList.js
let url_js = require("../../utils/url.js");
let common_js = require("../../utils/common.js");
let const_js = require("../../utils/const.js");
let user_js = require("../../utils/user.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");
let page_js = require("../../utils/pageOptions.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    contentStatus: -1,
    pageInfo: {
      curPage: 1,
      pageSize: 10,
      noMore: false
    },
    gbList: [],
    shareInfo: null, //分享信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //如果是分享进来的，并且没有选择角色，进入角色选择页面
    if (page_js.pageOptionsInfo()) {
      //2.relauch
      wx.reLaunch({
        url: '../indexSearchShop/indexSearchShop'
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  //初始化数据
  initData: function(options) {
    let tempUserInfo = user_js.userInfo();
    if (!user_js.hasRole()) {
      //1.save param
      let obj = {
        url: '../groupBuyingList/groupBuyingList',
        param: ''
      }
      page_js.savePageOptionsInfo(obj);
      //2.relauch
      //*********跳转放在onshow
      return;
    } else {
      //有角色，展示页面
      this.initUnShareOptionsData(options);
      //获取远程仓库数据
      this.getRemoteDatas(this.data.pageInfo.curPage, this.data.pageInfo.pageSize, true, true);
    }
  },

  //初始化options数据
  initUnShareOptionsData: function(options) {
    //userInfo
    let tempUserInfo = user_js.userInfo();
    this.setData({
      userInfo: tempUserInfo
    });
  },

  // 获取数据
  getRemoteDatas: function(curPage, pageSize, pullDownRefresh, withShareInfo) {
    let requestList = [];
    // 商品团购列表
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      curPage: curPage,
      pageSize: pageSize,
      shopId: this.data.userInfo.ztd.id
    };
    requestList.push(api.wxRequest(url_js.groupListUrl, data));
    //分享
    if (withShareInfo) {
      let shareData = {
        cityId: this.data.userInfo.ztd.cityId,
        code: this.data.userInfo.code,
        shopId: this.data.userInfo.ztd.id,
        type: 2
      };
      requestList.push(api.wxRequest(url_js.homeShareConfigUrl, shareData));
    }
    //获取数据
    Promise.all(requestList).then(results => {
      this.handleGroupListData(results[0], curPage, pullDownRefresh);
      if (withShareInfo) {
        this.handleHomeShareConfigData(results[1]);
      }
      common_js.hidePageLoading(pullDownRefresh);
    }).catch(err => {
      common_js.hidePageLoading(pullDownRefresh);
      common_js.log(err);
    });
  },

  //处理返回的商品列表数据
  handleGroupListData: function(res, curPage, pullDownRefresh) {
    //分享过来，如果当前点位不售卖分享过来的商品，回首页
    if (res.code == 704) {
      wx.switchTab({
        url: '../index/index'
      });
      return;
    }

    //设置数据
    let tmpList = this.data.gbList;
    if (pullDownRefresh) {
      tmpList = [];
    }
    //pageInfo
    let tmpPageInfo = this.data.pageInfo;
    if (res.page.pages <= curPage) {
      tmpPageInfo.noMore = true;
    } else {
      tmpPageInfo.curPage++;
    }
    //list
    if (res.result.length > 0) {
      tmpList = tmpList.concat(res.result);
      this.setData({
        contentStatus: 1,
        pageInfo: tmpPageInfo,
        gbList: tmpList
      });
    } else {
      this.setData({
        contentStatus: 1,
        pageInfo: tmpPageInfo
      });
    }
  },

  //处理返回的分享数据
  handleHomeShareConfigData: function(res) {
    this.setData({
      shareInfo: res.result
    });
    //1.开启转发
    wx.showShareMenu({});
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.contentStatus == -1) return;
    //2.reset data
    let tmpPageInfo = this.data.pageInfo;
    tmpPageInfo.curPage = 1;
    tmpPageInfo.noMore = false;
    this.setData({
      pageInfo: tmpPageInfo
    });
    this.getRemoteDatas(tmpPageInfo.curPage, tmpPageInfo.pageSize, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.pageInfo.noMore) return;
    //1. request
    this.getRemoteDatas(this.data.pageInfo.curPage, this.data.pageInfo.pageSize);
  },

  //去拼团
  onGb: function(e) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.groupBuyingListPt);

    let index = e.currentTarget.dataset.index;
    let item = this.data.gbList[index];
    wx.navigateTo({
      url: '../goodsDetails/goodsDetails?productId=' + item.id
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.groupBuyingShare);

    return this.shareAppMessage();
  },

  //获取分享的AppMessage
  shareAppMessage: function() {
    let tmpPath = '/pages/groupBuyingList/groupBuyingList';
    return {
      title: this.data.shareInfo ? this.data.shareInfo.title : '好想你心选',
      path: tmpPath,
      imageUrl: this.data.shareInfo ? this.data.shareInfo.href : ''
    }
  },
})